const polyv = require('../../utils/polyv.js');
var utilMd5 = require('../../utils/md5.js');
const app = getApp();
var timer,
    player,
    nnn1 = 0;
Page({
    onReady: function(res) {
        this.videoContext = wx.createVideoContext('polyvVideo');
    },
    data: {
        content: '自定义toast组件',
        iconfont: '',
        isHide: false,
        Descriptiontitle: '',
        coursestype: '',
        video: {},
        navTpye: false,
        comment: false,
        second: nnn1,
        tokenID: '',
        sign: '',
        ts: '',
        vid: '',
        sysid: '',
        gid: '',
        focus: false,
        hideHeader: true,
        hideBottom: true,
        contentlist: [],
        allPages: '',
        currentPage: 1,
        loadMoreData: '加载更多……',
        coursesOptional: false,
        optionalNumber: 0,
        appreciates: false,
        noappreciates: false,
        userCourseId: '',
        contents: '',
        cvalue: '',
        nice: 0,
        nonice: 0,
        PostComment: '',
        commentsNum: 0,
        scrollTop: 0
    },
    onLoad: function(options) {
        var _this = this;
        var vid = options.vid;
        var sysid = options.sysid;
        var token = wx.getStorageSync('token');
        _this.setData({
            tokenID: token,
            vid: options.vid,
            sysid: options.sysid,
            gid: options.gid,
            coursestype: options.type
        })
        wx.request({
            url: app.data.api + 'playvideo/getPlayParam',
            method: "POST",
            data: {
                "vid": _this.data.vid,
                "token": _this.data.tokenID,
                "onlineCourseId": _this.data.sysid,
                "type": _this.data.coursestype,
                "gid": _this.data.gid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                var vid = res.data.data.vid;
                var timestamp = Date.parse(new Date());
                var secretKey = "hxAuvuPQpR";
                var ts = timestamp;
                var sign = utilMd5.hexMD5(secretKey + vid + ts);
                var coursesOptional = res.data.data.coursesOptional;
                polyv.getVideo(vid, function(videoInfo) {
                    _this.setData({
                        video: {
                            src: videoInfo.src[0]
                        },
                        coursesOptional: coursesOptional,
                        userCourseId: res.data.data.userCourseId,
                        contents: res.data.data.onlineCourse.contents,
                        cvalue: res.data.data.onlineCourse.cvalue,
                        nice: res.data.data.onlineCourse.nice,
                        nonice: res.data.data.onlineCourse.nonice,
                        Descriptiontitle: options.title,
                        coursestype: options.type,
                        commentsNum: res.data.data.onlineCourse.comments,
                        optionalNumber: res.data.data.optionalNumber
                    });
                }, ts, sign);
                wx.setNavigationBarTitle({
                    title: options.title
                })
            }
        })
        _this.getData()
    },
    changetimeupdate: function() {
        this.setData({
            second: nnn1
        })
    },
    bindPlay: function() {
        this.videoContext.play()
        Countdown();
        this.setData({
            second: 0
        })
        this.getplaytime();
        nnn1 = 0;
    },
    bindPause: function() {
        this.videoContext.pause()
        clearTimeout(timer);
        this.setData({
            second: nnn1
        })
        this.getplaytime();
        nnn1 = 0;
    },

    onUnload: function() {
        clearTimeout(timer);
        this.getplaytime();
        nnn1 = 0;
    },
    changeTab: function(event) {
        var _this = this,
            type = event.target.dataset.type;
        if (type == 'true') {
            _this.setData({
                navTpye: true
            })
        } else {
            _this.setData({
                navTpye: false
            })
        }

    },
    showinput: function() {
        this.setData({
            focus: true
        })
    },
    hideinput: function() {
        this.setData({
            focus: false
        })
    },
    refresh: function() {
        var _this = this;
        setTimeout(function() {
            _this.setData({
                currentPage: 1,
                hideHeader: false
            })
            _this.getData();
        }, 300);
    },
    loadMore: function() {
        var self = this;
        if (self.data.currentPage == self.data.allPages) {
            self.setData({
                loadMoreData: '==我是有底线的=='
            })
            return;
        }
        setTimeout(function() {
            var tempCurrentPage = self.data.currentPage;
            tempCurrentPage = tempCurrentPage + 1;
            self.setData({
                currentPage: tempCurrentPage,
                hideBottom: false
            })
            self.getData();
        }, 500);
    },
    getData: function() {
        var self = this;
        var pageIndex = self.data.currentPage;
        wx.request({
            url: app.data.api + 'playvideo/getComment',
            data: {
                "token": self.data.tokenID,
                "onlineCourseId": self.data.sysid,
                "page": self.data.currentPage,
            },
            success: function(res) {
                setTimeout(function() {
                    var dataModel = res.data.data;
                    if (pageIndex == 1) {
                        self.setData({
                            allPages: dataModel.pages,
                            contentlist: dataModel.list,
                            hideHeader: true
                        })
                    } else {
                        var tempArray = self.data.contentlist;
                        tempArray = tempArray.concat(dataModel.list);
                        self.setData({
                            allPages: dataModel.pages,
                            contentlist: tempArray,
                            hideBottom: true
                        })
                    }
                }, 800);
            },
            fail: function() {

            }
        })
    },
    coursetap: function() {
        this.setData({
            comment: true
        })
    },
    PostComment: function(e) {
        this.setData({
            PostComment: e.detail.value
        })
    },
    submitBtn: function() {
        var _this = this;
        if (_this.data.PostComment.length == 0) {
            _this.layerMsg(true, '请输入评论内容', 'icon-weiwancheng');
            return false
        }
        wx.request({
            url: app.data.api + 'playvideo/comment',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
                "text": _this.data.PostComment,
                "onlineCourseId": _this.data.sysid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    setTimeout(function() {
                        _this.layerMsg(true, res.data.msg, 'icon-yiwancheng');
                    }, 800)
                    _this.setData({
                        currentPage: 1,
                        commentsNum: _this.data.commentsNum + 1,
                        hideHeader: false
                    })
                    _this.getData();
                    _this.goscrollTop();
                }
            },
            fail: function() {

            }
        })
    },
    getplaytime: function() {
        var _this = this;
        wx.request({
            url: app.data.api + 'playvideo/saveOrUpdatePlayIncentive',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
                "usercourseid": _this.data.userCourseId,
                "time": _this.data.second,
                "type": _this.data.coursestype
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {

            },
            fail: function() {

            }
        })
    },
    taptake: function() {
        var _this = this;
        wx.request({
            url: app.data.api + 'playvideo/addOptional',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
                "onlineCourseId": _this.data.sysid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status == true) {

                    _this.setData({
                        coursesOptional: !_this.data.coursesOptional,
                        optionalNumber: _this.data.optionalNumber + 1
                    })

                    _this.layerMsg(true, res.data.msg, 'icon-yiwancheng');
                } else {
                    _this.layerMsg(true, res.data.msg, 'icon-weiwancheng');
                }

            }
        })
    },
    appreciates: function(event) {
        var _this = this;
        var type = event.currentTarget.dataset.id;
        var istype = event.currentTarget.dataset.type;
        wx.request({
            url: app.data.api + 'playvideo/nice',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
                "onlineCourseId": _this.data.sysid,
                "type": istype
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status == true) {
                    if (type == 'up') {
                        _this.setData({
                            appreciates: !_this.data.appreciates,
                            nice: _this.data.nice + 1
                        })
                    } else if (type == 'down') {
                        _this.setData({
                            noappreciates: !_this.data.noappreciates,
                            nonice: _this.data.nonice + 1
                        })
                    }
                    _this.layerMsg(true, res.data.msg, 'icon-yiwancheng');
                } else {
                    _this.layerMsg(true, res.data.msg, 'icon-weiwancheng');
                }

            }
        })
    },
    layerMsg: function(msgtype, msg, iconfont) {
        let $this = this;
        if (msgtype == true) {
            $this.setData({
                isHide: msgtype,
                content: msg,
                iconfont: iconfont
            })
            setTimeout(function() {
                $this.setData({
                    isHide: false
                })
            }, 2000)
        } else {}
    },
    goscrollTop: function(e) {
        this.setData({
            scrollTop: 0
        })
    }
})

function Countdown() {
    timer = setTimeout(function() {
        nnn1++
        Countdown();
    }, 1000);
};